class ArtifactsController < ApplicationController
  layout  'prometheus'
  
  def add_context
    if params[:list_items].nil?
      params[:list_items] = [params[:list_item]]
    end
    list_items = params[:list_items].split('/')
    list_items.each do |item|
      artifact = Artifact.find(item)
      # doesn't make sense to have the next line, but it appears to be necessary
      artifact = artifact[0] if artifact.class == Array
      if artifact.context.nil?
        context = Context.new(:artifact_id => artifact.id, :user_id => session_user.id) 
        context.save!
      end
    end
    context_present
    Recent.add(artifact,session_user) rescue nil
    #FlickrImage.search(@session_user,@context_elements)
    @artifacts = Artifact.get_artifacts(session_user, @context_elements)
    respond_to do |format|
      format.js { render :action => 'update_panes', :type => 'rjs'}
      format.xhtml { render :template => 'artifacts/index.rxhtml', :layout=>false }
    end
  end
  
  def add_recent
    artifact = Artifact.find(params[:list_item])
    add_to_recent(artifact,session_user)
    @artifacts = Artifact.get_artifacts(session_user, @context_elements)
    render :action => 'update_panes', :type => 'rjs'
  end
  
  def add_relations
    if params[:list_items].nil?
      params[:list_items] = [params[:list_item]]
    end
    list_items = params[:list_items].split('/')
    # add relationship between object and contents of context
    list_items.each do |item|
      object = Artifact.find(item)
      # doesn't make sense to have the next line, but it appears to be necessary
      object = object[0] if object.class == Array
      Relation.add_relation_to_context(object,session_user, @context_elements)
    end
    @artifacts = Artifact.get_artifacts(session_user, @context_elements)
    logger.info @context_present
    render :action => 'update_panes', :type => 'rjs'  
  end
  
  def remove_relations
    # remove relationship between object and contents of context
    object = Artifact.find(params[:id])
    Relation.remove_relation_to_context(object,session_user, @context_elements)
    @artifacts = Artifact.get_artifacts(session_user, @context_elements)
    render :action => 'update_panes', :type => 'rjs'  
  end  
  
  # GET /artifacts
  # GET /artifacts.xml
  def index
    @artifacts = Artifact.get_artifacts(session_user, @context_elements, params[:type], params[:query])
    respond_to do |format|
      format.html # index.rhtml
      format.xhtml { render :template => 'artifacts/index.rxhtml', :layout=>false }
      format.xml  { render :xml => @artifacts.to_xml }
    end
  end
  
  # GET /artifacts/1
  # GET /artifacts/1.xml
  def show
    @artifact = Artifact.find(params[:id])
    respond_to do |format|
      format.html # show.rhtml
      format.xhtml { render :template => 'artifacts/show.rxhtml', :layout=>false }
      format.xml  { render :xml => @artifact.to_xml }
    end
  end
  
  # GET /artifacts/new
  def new
    @artifact = Artifact.new
    respond_to do |format|
      format.html # new.rhtml
      format.xhtml { render :template => 'artifacts/new.rxhtml', :layout=>false }
      format.xml  { render :xml => @artifacts.to_xml }
    end
  end
  
  # GET /artifacts/1;edit
  def edit
    @artifact = Artifact.find(params[:id])
    respond_to do |format|
      format.html # edit.rhtml
      format.xhtml { render :template => 'artifacts/edit.rxhtml', :layout=>false }
      format.xml  { render :xml => @artifact.to_xml }
    end
  end
  
  # POST /artifacts
  # POST /artifacts.xml
  def create
    @artifact = Artifact.new(params[:artifact])
    save = @artifact.save
    Relation.add_relation_to_context(@artifact,session_user, @context_elements)
    Recent.add(@artifact,session_user)
    
    respond_to do |format|
      if save
        flash[:notice] = 'Artifact was successfully created.'
        format.html { redirect_to :action => 'index' }
        format.xml  { head :created, :location => artifact_url(@artifact) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @artifact.errors.to_xml }
      end
    end
  end
  
  # PUT /artifacts/1
  # PUT /artifacts/1.xml
  def update
    @artifact = Artifact.find(params[:id])
    
    respond_to do |format|
      if @artifact.update_attributes(params[:artifact])
        flash[:notice] = 'Artifact was successfully updated.'
        format.html { redirect_to artifact_url(@artifact) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @artifact.errors.to_xml }
      end
    end
  end
  
  # DELETE /artifacts/1
  # DELETE /artifacts/1.xml
  def destroy
    @artifact = Artifact.find(params[:id])
    @artifact.destroy
    
    respond_to do |format|
      format.html { redirect_to artifacts_url }
      format.xml  { head :ok }
    end
  end
  
  # my_discourse /artifacts/mydiscourse
  def my_discourse
    @workspaces = Artifact.get_artifacts(session_user, @context_elements, 'Workspace', params[:query])   
    @resources = Artifact.get_artifacts(session_user, @context_elements, 'Resource', params[:query])   
    respond_to do |format|
      format.html # my_discourse.rhtml
      format.xhtml { render :template => 'artifacts/my_discourse.rxhtml', :layout=>false }
      format.xml  { render :xml => @artifact.to_xml }
    end
  end
end

